﻿using JinJun.EF.Permission.BLL;
using JinJun.EF.Permission.Model;
using JinJun.EF.Permission.Web.Attributes;
using JinJun.EF.Permission.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace JinJun.EF.Permission.Web.Controllers
{
    [PermissionAttribute]
    public class ModuleController : Controller
    {
        //创建业务层对象
        private ModuleService moduleService = new ModuleService();

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public JsonResult GetModuleList(int page, int limit, string name)
        {
            //创建分页类对象
            PageListResult<Module> pageListResult = new PageListResult<Module>();
            //赋值
            pageListResult.code = 0;
            pageListResult.msg = string.Empty;
            int count = 0;
            //创建以Module为类型的List对象
            List<Module> modules = new List<Module>();
            //表达式
            Expression<Func<Module, bool>> whereLambda = a => a.Name.Contains(name);
            Expression<Func<Module, int>> orderbyLambda = a => a.Id;
            //调用分页查询
            pageListResult.data = moduleService.QueryPageList(page, limit, whereLambda, orderbyLambda, out count);
            return Json(pageListResult);
        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult Update(Module module)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = moduleService.Update(module);
            return Json(operateResult);
        }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult AddModule(Module module)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = moduleService.Add(module);
            return Json(operateResult);
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult Delete(Module module)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = moduleService.Delete(module);
            return Json(operateResult);
        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult BatchDelete(List<Module> modules)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = moduleService.BatchDelete(modules);
            return Json(operateResult);
        }


        /// <summary>
        /// 删除多个实体根据Id
        /// </summary>
        /// <param name="module"></param>
        /// <returns></returns>
        public JsonResult BatchDeleteById(int[] ids)
        {
            //创建操作类对象
            OperateResult operateResult = new OperateResult();
            //调用业务层添加方法
            operateResult.Success = moduleService.BatchDeleteById(ids);
            return Json(operateResult);
        }
    }
}